<template>
  <div class="login-container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form
          :model="loginFrom"
          :rules="rules"
          class="login_form"
          ref="loginForms"
        >
          <h1>后台管理系统</h1>
          <h2>
            最爱喝兽奶
            <span style="color: hotpink">后台管理系统</span>
          </h2>
          <el-form-item prop="username">
            <el-input
              v-model="loginFrom.username"
              :prefix-icon="User"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              type="password"
              v-model="loginFrom.password"
              :show-password="'true'"
              :prefix-icon="Lock"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              :loading="loading"
              class="login_btn"
              type="primary"
              size="default"
              @click="login"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { User, Lock } from '@element-plus/icons-vue'
import { reactive, ref } from 'vue'
import { ElNotification } from 'element-plus'
// 引入用户相关的小仓库
import useUserStore from '@/store/modules/user.ts'
import { useRouter } from 'vue-router'
// 引入时间状态函数
import { getTime } from '@/utils/time.ts'
let useStore = useUserStore()
// 获取路由器
let $router = useRouter()
// 登录加载效果
let loading = ref(false)
// 获取el-from组件
let loginForms = ref()
// 收集账号密码的数据
let loginFrom = reactive({
  username: 'admin',
  password: '111111',
})
// 自定义表单校验规则方法
let validatorUserName = (rule: any, value: any, callback: any) => {
  console.log(rule)
  // rule: 即为校验规则对象
  // value: 即为表单元素文本内容
  // callback: 如果符合条件callback放行通过
  // 如果不符合条件callback方法，注入错误信息
  if (value.length >= 5 && value.length <= 10) {
    callback()
  } else {
    callback(new Error('账号长度需在5~10位之间'))
  }
}
let validatorPassWord = (rule: any, value: any, callback: any) => {
  console.log(rule)
  if (value.length >= 6 && value.length <= 15) {
    callback()
  } else {
    callback(new Error('密码长度需在6~15位之间'))
  }
}
// 定义表单校验需要的数据
const rules = reactive({
  // 规则对象属性
  // required: 代表这个字段必须进行校验
  // min: 代表表单最少多少位
  // max: 代表表单最长多少位
  // message: 错误的提示消息
  // trigger: 触发校验表单的时机，change=>文本发生变化触发校验,blur=>失去焦点的时候触发校验规则
  username: [
    // {required:true,min: 6, max: 10, message: '账号长度至少六位', trigger: 'change' },
    // 自定义校验规则
    { trigger: 'change', validator: validatorUserName },
  ],
  password: [
    // {required:true,min: 6, max: 15, message: '密码长度至少六位', trigger: 'change' },
    { trigger: 'change', validator: validatorPassWord },
  ],
})
// 登录按钮的回调
const login = async () => {
  // 保证表单校验全部通过在发送请求
  await loginForms.value.validate()
  // 加载效果：点击登录触发
  loading.value = true
  try {
    // 保证登录成功
    await useStore.userLogin(loginFrom)
    // 编程试导航跳转到展示数据首页
    await $router.push('/')
    // 登录成功的提示信息
    ElNotification({
      type: 'success',
      message: '欢迎来到九天十地！！！',
      title: `Hi,${getTime()}好`,
    })
    // 登录成功取消登录加载效果
    loading.value = false
  } catch (e) {
    // 登录失败的提示信息
    ElNotification({
      type: 'error',
      message: (e as Error).message,
    })
    // 登录失败取消登录加载效果
    loading.value = false
  }
}
</script>

<style scoped lang="scss">
.login-container {
  height: 100vh;
  width: 100%;
  background-color: skyblue;
  background-image: url('@/assets/images/background.jpg');
  background-size: cover;
  .login_form {
    padding: 40px;
    position: relative;
    width: 80%;
    top: 30vh;
    background-image: url('@/assets/images/login_form.png');
    background-size: cover;
    h1 {
      color: #fff;
      font-size: 40px;
    }
    h2 {
      font-size: 20px;
      color: #fff;
      margin: 20px 0;
    }
    .login_btn {
      width: 100%;
    }
  }
}
</style>
